<template>
  <ul class="list">
    <!-- <li class="item" v-for="(item, key) of cities" :key=key>{{key}}</li> -->
    <li class="item" v-for="item of letters" :key="item"
    :ref="item"
    @click="handLetterClick"
    @touchstart="handleLetterTouchStart"
    @touchmove="handleLetterTouchMove"
    @touchend="handleLetterTouchEnd"
    >{{item}}</li>
  </ul>
</template>

<script>
export default {
  name: 'CityAlphabet',
  props: {
    cities: Object
  },
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timmer: null
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  computed: {
    letters () {
      const letters = []
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  methods: {
    handLetterClick (e) {
      this.$emit('change', e.target.innerText)
    },
    handleLetterTouchStart () {
      this.touchStatus = true
    },
    handleLetterTouchMove (e) {
      if (this.touchStatus) {
        if (this.timmer) {
          clearTimeout(this.timmer)
        }
        this.timmer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 79
          let i = Math.floor((touchY - this.startY) / 22)
          if (i >= 0 && i < this.letters.length) {
            this.$emit('change', this.letters[i])
          }
        }, 16)
      }
    },
    handleLetterTouchEnd () {
      this.touchStatus = false
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl'
.list
    //上下居中
    //     display flex
    // flex-direction column
    // justify-content center
    display flex
    flex-direction column
    justify-content center
    position absolute
    top 1.58rem
    right 0
    bottom 0
    width .4rem
    .item
        line-height .44rem
        text-align center
        color $backgroundColor
</style>
